home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint96sb.zoo / src / dos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-21  |  12.0 KB  |  556 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith. All rights reserved.
  3. */
  4.  
  5. /* miscellaneous DOS functions, and the DOS initialization function */
  6.  
  7. #include "mint.h"
  8.  
  9. #define DOS_MAX 0x140
  10.  
  11. Func dos_tab[DOS_MAX];
  12. short dos_max = DOS_MAX;
  13.  
  14. static void alarmme P_((PROC *));
  15.  
  16. long ARGS_ON_STACK 
  17. s_version()
  18. {
  19.     return Sversion();
  20. }
  21.  
  22. /*
  23.  * Super(new_ssp): change to supervisor mode.
  24.  */
  25.  
  26. long ARGS_ON_STACK
  27. s_uper(new_ssp)
  28.     long new_ssp;
  29. {
  30.     int in_super;
  31.     long r;
  32.  
  33.     TRACE(("Super"));
  34.     in_super = curproc->ctxt[SYSCALL].sr & 0x2000;
  35.  
  36.     if (new_ssp == 1) {
  37.         r = in_super ? -1L : 0;
  38.     }
  39.     else {
  40.         curproc->ctxt[SYSCALL].sr ^= 0x2000;
  41.         r = curproc->ctxt[SYSCALL].ssp;
  42.         if (in_super) {
  43.             if (new_ssp == 0) {
  44.                 DEBUG(("bad Super call"));
  45.                 raise(SIGSYS);
  46.             }
  47.             else {
  48.                 curproc->ctxt[SYSCALL].usp = 
  49.                     curproc->ctxt[SYSCALL].ssp;
  50.                 curproc->ctxt[SYSCALL].ssp = new_ssp;
  51.             }
  52.         }
  53.         else {
  54.             curproc->ctxt[SYSCALL].ssp = 
  55.                 new_ssp ? new_ssp : curproc->ctxt[SYSCALL].usp;
  56.         }
  57.     }
  58.     return r;
  59. }
  60.  
  61. /*
  62.  * get/set time and date functions
  63.  */
  64. long ARGS_ON_STACK t_getdate() { return datestamp; }
  65. long ARGS_ON_STACK t_gettime() { return timestamp; }
  66.  
  67. long ARGS_ON_STACK t_setdate(date)
  68.     int date;
  69. {
  70.     long r = Tsetdate(date);
  71.     datestamp = Tgetdate();
  72.     return r;
  73. }
  74.  
  75. long ARGS_ON_STACK t_settime(time)
  76.     int time;
  77. {
  78.     long r = Tsettime(time);
  79.     timestamp = Tgettime();
  80.     return r;
  81. }
  82.  
  83. /*
  84.  * GEMDOS extension: Syield(): give up the processor if any other
  85.  * processes are waiting. Always returns 0.
  86.  */
  87.  
  88. long ARGS_ON_STACK
  89. s_yield()
  90. {
  91. /* reward the nice process */
  92.     curproc->curpri = curproc->pri;
  93.     sleep(READY_Q, curproc->wait_cond);
  94.     return 0;
  95. }
  96.  
  97. /*
  98.  * GEMDOS extension:
  99.  * Prenice(pid, delta) sets the process priority level for process pid.
  100.  * A "nice" value < 0 increases priority, one > 0 decreases it.
  101.  * Always returns the new priority (so Prenice(pid, 0) queries the current
  102.  * priority).
  103.  *
  104.  * NOTE: for backward compatibility, Pnice(delta) is provided and is equivalent
  105.  * to Prenice(Pgetpid(), delta)
  106.  */
  107.  
  108. long ARGS_ON_STACK
  109. p_renice(pid, delta)
  110.     int pid, delta;
  111. {
  112.     PROC *p;
  113.  
  114.     if (pid <= 0 || 0 == (p = pid2proc(pid))) {
  115.         return EFILNF;
  116.     }
  117.  
  118.     if (curproc->euid && curproc->euid != p->ruid
  119.         && curproc->ruid != p->ruid) {
  120.         DEBUG(("Prenice: process ownership error"));
  121.         return EACCDN;
  122.     }
  123.     p->pri -= delta;
  124.     if (p->pri < MIN_NICE) p->pri = MIN_NICE;
  125.     if (p->pri > MAX_NICE) p->pri = MAX_NICE;
  126.     p->curpri = p->pri;
  127.     return ((long)p->pri) & 0x0ffff;
  128. }
  129.  
  130. long ARGS_ON_STACK
  131. p_nice(delta)
  132.     int delta;
  133. {
  134.     return p_renice(curproc->pid,delta);
  135. }
  136.  
  137. /*
  138.  * GEMDOS extensions: routines for getting/setting process i.d.'s and
  139.  * user i.d.'s
  140.  */
  141.  
  142. long ARGS_ON_STACK p_getpid() { return curproc->pid; }
  143.  
  144. long ARGS_ON_STACK p_getppid() { return curproc->ppid; }
  145.  
  146. long ARGS_ON_STACK p_getpgrp() { return curproc->pgrp; }
  147.  
  148. /* note: Psetpgrp(0, ...) is equivalent to Psetpgrp(Pgetpid(), ...) */
  149. /* also note: Psetpgrp(x, 0) is equivalent to Psetpgrp(x, x) */
  150.  
  151. long ARGS_ON_STACK p_setpgrp(pid, newgrp)
  152.     int pid, newgrp;
  153. {
  154.     PROC *p;
  155.  
  156.     if (pid == 0)
  157.         p = curproc;
  158.     else if (0 == (p = pid2proc(pid)))
  159.         return EFILNF;
  160.     if ( (curproc->euid) && (p->ruid != curproc->ruid)
  161.           && (p->ppid != curproc->pid) )
  162.         return EACCDN;
  163.  
  164.     if (newgrp == 0)
  165.         newgrp = p->pid;
  166.  
  167.     return (p->pgrp = newgrp);
  168. }
  169.  
  170. long ARGS_ON_STACK p_getuid() { return curproc->ruid; }
  171. long ARGS_ON_STACK p_getgid() { return curproc->rgid; }
  172. long ARGS_ON_STACK p_geteuid() { return curproc->euid; }
  173. long ARGS_ON_STACK p_getegid() { return curproc->egid; }
  174.  
  175. long ARGS_ON_STACK
  176. p_setuid(id)
  177.     int id;
  178. {
  179.     if (curproc->euid == 0 || curproc->ruid == id) {
  180.         curproc->ruid = curproc->euid = id;
  181.         return id;
  182.     }
  183.     return EACCDN;
  184. }
  185.  
  186. long ARGS_ON_STACK
  187. p_setgid(id)
  188.     int id;
  189. {
  190.     if (curproc->euid == 0 || curproc->egid == 0 || curproc->rgid == id) {
  191.         curproc->egid = curproc->rgid = id;
  192.         return id;
  193.     }
  194.     return EACCDN;
  195. }
  196.  
  197. /*
  198.  * a way to get/set process-specific user information. the user information
  199.  * longword is set to "arg", unless arg is -1. In any case, the old
  200.  * value of the longword is returned.
  201.  */
  202.  
  203. long ARGS_ON_STACK
  204. p_usrval(arg)
  205.     long arg;
  206. {
  207.     long r;
  208.  
  209.     TRACE(("Pusrval"));
  210.     r = curproc->usrdata;
  211.     if (arg != -1L)
  212.         curproc->usrdata = arg;
  213.     return r;
  214. }
  215.  
  216. /*
  217.  * set the file creation mask to "mode". Returns the old value of the
  218.  * mask.
  219.  */
  220. long ARGS_ON_STACK p_umask(mode)
  221.     unsigned mode;
  222. {
  223.     long oldmask = curproc->umask;
  224.  
  225.     curproc->umask = mode & (~S_IFMT);
  226.     return oldmask;
  227. }
  228.  
  229. /*
  230.  * get/set the domain of a process. domain 0 is the default (TOS) domain.
  231.  * domain 1 is the MiNT domain. for now, domain affects read/write system
  232.  * calls and filename translation.
  233.  */
  234.  
  235. long ARGS_ON_STACK
  236. p_domain(arg)
  237.     int arg;
  238. {
  239.     long r;
  240.     TRACE(("Pdomain(%d)", arg));
  241.  
  242.     r = curproc->domain;
  243.     if (arg >= 0)
  244.         curproc->domain = arg;
  245.     return r;
  246. }
  247.  
  248. /*
  249.  * get process resource usage. 8 longwords are returned, as follows:
  250.  *     r[0] == system time used by process
  251.  *     r[1] == user time used by process
  252.  *     r[2] == system time used by process' children
  253.  *     r[3] == user time used by process' children
  254.  *     r[4] == memory used by process
  255.  *     r[5] - r[7]: reserved for future use
  256.  */
  257.  
  258. long ARGS_ON_STACK
  259. p_rusage(r)
  260.     long *r;
  261. {
  262.     r[0] = curproc->systime;
  263.     r[1] = curproc->usrtime;
  264.     r[2] = curproc->chldstime;
  265.     r[3] = curproc->chldutime;
  266.     r[4] = memused(curproc);
  267.     return 0;
  268. }
  269.  
  270. /*
  271.  * get/set resource limits i to value v. The old limit is always returned;
  272.  * if v == -1, the limit is unchanged, otherwise it is set to v. Possible
  273.  * values for i are:
  274.  *    1:  max. cpu time    (milliseconds)
  275.  *    2:  max. core memory allowed
  276.  *    3:  max. amount of malloc'd memory allowed
  277.  */
  278. long ARGS_ON_STACK
  279. p_setlimit(i, v)
  280.     int i;
  281.     long v;
  282. {
  283.     long oldlimit;
  284.  
  285.     switch(i) {
  286.     case 1:
  287.         oldlimit = curproc->maxcpu;
  288.         if (v >= 0) curproc->maxcpu = v;
  289.         break;
  290.     case 2:
  291.         oldlimit = curproc->maxcore;
  292.         if (v >= 0) {
  293.             curproc->maxcore = v;
  294.             recalc_maxmem(curproc);
  295.         }
  296.         break;
  297.     case 3:
  298.         oldlimit = curproc->maxdata;
  299.         if (v >= 0) {
  300.             curproc->maxdata = v;
  301.             recalc_maxmem(curproc);
  302.         }
  303.         break;
  304.     default:
  305.         DEBUG(("Psetlimit: invalid mode %d", i));
  306.         return EINVFN;
  307.     }
  308.     TRACE(("p_setlimit(%d, %ld): oldlimit = %ld", i, v, oldlimit));
  309.     return oldlimit;
  310. }
  311.  
  312. /*
  313.  * pause: just sleeps on IO_Q, with wait_cond == -1. only a signal will
  314.  * wake us up
  315.  */
  316.  
  317. long ARGS_ON_STACK
  318. p_pause()
  319. {
  320.     TRACE(("Pause"));
  321.     sleep(IO_Q, -1L);
  322.     return 0;
  323. }
  324.  
  325. /*
  326.  * helper function for t_alarm: this will be called when the timer goes
  327.  * off, and raises SIGALRM
  328.  */
  329.  
  330. static void
  331. alarmme(p)
  332.     PROC *p;
  333. {
  334.     p->alarmtim = 0;
  335.     post_sig(p, SIGALRM);
  336. }
  337.  
  338. /*
  339.  * t_alarm(x): set the alarm clock to go off in "x" seconds. returns the
  340.  * old value of the alarm clock
  341.  */
  342.  
  343. long ARGS_ON_STACK
  344. t_alarm(x)
  345.     long x;
  346. {
  347.     long oldalarm;
  348.     TIMEOUT *t;
  349.  
  350. /* see how many milliseconds there were to the alarm timeout */
  351.     oldalarm = 0;
  352.  
  353.     if (curproc->alarmtim) {
  354.         for (t = tlist; t; t = t->next) {
  355.             oldalarm += t->when;
  356.             if (t == curproc->alarmtim)
  357.                 goto foundalarm;
  358.         }
  359.         DEBUG(("Talarm: old alarm not found!"));
  360.         oldalarm = 0;
  361.         curproc->alarmtim = 0;
  362. foundalarm:
  363.         ;
  364.     }
  365.  
  366.     oldalarm = (oldalarm+999) / 1000;    /* convert to seconds */
  367.  
  368. /* we were just querying the alarm */
  369.     if (x < 0)
  370.         return oldalarm;
  371.  
  372. /* cancel old alarm */
  373.     if (curproc->alarmtim)
  374.         canceltimeout(curproc->alarmtim);
  375.  
  376. /* add a new alarm, to occur in 1000*x milliseconds */
  377.     if (x)
  378.         curproc->alarmtim = addtimeout(1000*x, alarmme);
  379.     else
  380.         curproc->alarmtim = 0;
  381.  
  382.     return oldalarm;
  383. }
  384.  
  385. /*
  386.  * sysconf(which): returns information about system configuration.
  387.  * "which" specifies which aspect of the system configuration is to
  388.  * be returned:
  389.  *    -1    max. value of "which" allowed
  390.  *    0    max. number of memory regions per proc
  391.  *    1    max. length of Pexec() execution string {ARG_MAX}
  392.  *    2    max. number of open files per process    {OPEN_MAX}
  393.  *    3    number of supplementary group id's    {currently 0}
  394.  *    4    max. number of processes per uid    {CHILD_MAX}
  395.  *
  396.  * unlimited values (e.g. CHILD_MAX) are returned as 0x7fffffffL
  397.  *
  398.  * See also Dpathconf() in dosdir.c.
  399.  */
  400.  
  401. long ARGS_ON_STACK
  402. s_ysconf(which)
  403.     int which;
  404. {
  405.     if (which == -1)
  406.         return 4;
  407.  
  408.     switch(which) {
  409.         case 0:
  410.             return UNLIMITED;
  411.         case 1:
  412.             return 126;
  413.         case 2:
  414.             return MAX_OPEN;
  415.         case 3:
  416.             return 0;
  417.         case 4:
  418.             return UNLIMITED;
  419.         default:
  420.             return EINVFN;
  421.     }
  422. }
  423.  
  424. /*
  425.  * routine for initializing DOS
  426.  *
  427.  * NOTE: before adding new functions, check the definition of
  428.  * DOS_MAX at the top of this file to make sure that there
  429.  * is room; if not, increase DOS_MAX.
  430.  */
  431.  
  432. void
  433. init_dos()
  434. {
  435. /* miscellaneous initialization goes here */
  436.     timestamp = Tgettime();
  437.     datestamp = Tgetdate();
  438.  
  439. /* dos table initialization */
  440.     dos_tab[0x00] = p_term0;
  441.     dos_tab[0x01] = c_conin;
  442.     dos_tab[0x02] = c_conout;
  443.     dos_tab[0x03] = c_auxin;
  444.     dos_tab[0x04] = c_auxout;
  445.     dos_tab[0x05] = c_prnout;
  446.     dos_tab[0x06] = c_rawio;
  447.     dos_tab[0x07] = c_rawcin;
  448.     dos_tab[0x08] = c_necin;
  449.     dos_tab[0x09] = c_conws;
  450.     dos_tab[0x0a] = c_conrs;
  451.     dos_tab[0x0b] = c_conis;
  452.     dos_tab[0x0e] = d_setdrv;
  453.     dos_tab[0x10] = c_conos;
  454.     dos_tab[0x11] = c_prnos;
  455.     dos_tab[0x12] = c_auxis;
  456.     dos_tab[0x13] = c_auxos;
  457.     dos_tab[0x14] = m_addalt;
  458.     dos_tab[0x19] = d_getdrv;
  459.     dos_tab[0x1a] = f_setdta;
  460.     dos_tab[0x20] = s_uper;
  461.     dos_tab[0x2a] = t_getdate;
  462.     dos_tab[0x2b] = t_setdate;
  463.     dos_tab[0x2c] = t_gettime;
  464.     dos_tab[0x2d] = t_settime;
  465.     dos_tab[0x2f] = f_getdta;
  466.     dos_tab[0x30] = s_version;
  467.     dos_tab[0x31] = p_termres;
  468.     dos_tab[0x36] = d_free;
  469.     dos_tab[0x39] = d_create;
  470.     dos_tab[0x3a] = d_delete;
  471.     dos_tab[0x3b] = d_setpath;
  472.     dos_tab[0x3c] = f_create;
  473.     dos_tab[0x3d] = f_open;
  474.     dos_tab[0x3e] = f_close;
  475.     dos_tab[0x3f] = f_read;
  476.     dos_tab[0x40] = f_write;
  477.     dos_tab[0x41] = f_delete;
  478.     dos_tab[0x42] = f_seek;
  479.     dos_tab[0x43] = f_attrib;
  480.     dos_tab[0x44] = m_xalloc;
  481.     dos_tab[0x45] = f_dup;
  482.     dos_tab[0x46] = f_force;
  483.     dos_tab[0x47] = d_getpath;
  484.     dos_tab[0x48] = m_alloc;
  485.     dos_tab[0x49] = m_free;
  486.     dos_tab[0x4a] = m_shrink;
  487.     dos_tab[0x4b] = p_exec;
  488.     dos_tab[0x4c] = p_term;
  489.     dos_tab[0x4e] = f_sfirst;
  490.     dos_tab[0x4f] = f_snext;
  491.     dos_tab[0x56] = f_rename;
  492.     dos_tab[0x57] = f_datime;
  493.     dos_tab[0x5c] = f_lock;
  494.  
  495. /* MiNT extensions to GEMDOS */
  496.  
  497.     dos_tab[0xff] = s_yield;
  498.     dos_tab[0x100] = f_pipe;
  499.     dos_tab[0x104] = f_cntl;
  500.     dos_tab[0x105] = f_instat;
  501.     dos_tab[0x106] = f_outstat;
  502.     dos_tab[0x107] = f_getchar;
  503.     dos_tab[0x108] = f_putchar;
  504.     dos_tab[0x109] = p_wait;
  505.     dos_tab[0x10a] = p_nice;
  506.     dos_tab[0x10b] = p_getpid;
  507.     dos_tab[0x10c] = p_getppid;
  508.     dos_tab[0x10d] = p_getpgrp;
  509.     dos_tab[0x10e] = p_setpgrp;
  510.     dos_tab[0x10f] = p_getuid;
  511.     dos_tab[0x110] = p_setuid;
  512.     dos_tab[0x111] = p_kill;
  513.     dos_tab[0x112] = p_signal;
  514.     dos_tab[0x113] = p_vfork;
  515.     dos_tab[0x114] = p_getgid;
  516.     dos_tab[0x115] = p_setgid;
  517.     dos_tab[0x116] = p_sigblock;
  518.     dos_tab[0x117] = p_sigsetmask;
  519.     dos_tab[0x118] = p_usrval;
  520.     dos_tab[0x119] = p_domain;
  521.     dos_tab[0x11a] = p_sigreturn;
  522.     dos_tab[0x11b] = p_fork;
  523.     dos_tab[0x11c] = p_wait3;
  524.     dos_tab[0x11d] = f_select;
  525.     dos_tab[0x11e] = p_rusage;
  526.     dos_tab[0x11f] = p_setlimit;
  527.     dos_tab[0x120] = t_alarm;
  528.     dos_tab[0x121] = p_pause;
  529.     dos_tab[0x122] = s_ysconf;
  530.     dos_tab[0x123] = p_sigpending;
  531.     dos_tab[0x124] = d_pathconf;
  532.     dos_tab[0x125] = p_msg;
  533.     dos_tab[0x126] = f_midipipe;
  534.     dos_tab[0x127] = p_renice;
  535.     dos_tab[0x128] = d_opendir;
  536.     dos_tab[0x129] = d_readdir;
  537.     dos_tab[0x12a] = d_rewind;
  538.     dos_tab[0x12b] = d_closedir;
  539.     dos_tab[0x12c] = f_xattr;
  540.     dos_tab[0x12d] = f_link;
  541.     dos_tab[0x12e] = f_symlink;
  542.     dos_tab[0x12f] = f_readlink;
  543.     dos_tab[0x130] = d_cntl;
  544.     dos_tab[0x131] = f_chown;
  545.     dos_tab[0x132] = f_chmod;
  546.     dos_tab[0x133] = p_umask;
  547.     dos_tab[0x134] = p_semaphore;
  548.     dos_tab[0x135] = d_lock;
  549.     dos_tab[0x136] = p_sigpause;
  550.     dos_tab[0x137] = p_sigaction;
  551.     dos_tab[0x138] = p_geteuid;
  552.     dos_tab[0x139] = p_getegid;
  553.     dos_tab[0x13a] = p_waitpid;
  554.     dos_tab[0x13b] = d_getcwd;
  555. }
  556.